package com.sc.web.member;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.ParamMap;
import com.sc.dal.model.member.Member;
import com.sc.service.member.MemberGradeService;
import com.sc.service.member.MemberService;
import com.sc.util.data.Constants;
import com.sc.util.data.DataUtil;
import com.sc.util.data.MD5Util;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;
import com.sc.web.BaseController;
import com.sc.web.plugin.PageView;
@Controller
@RequestMapping(value="/member")
public class MemberController extends BaseController{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberGradeService memberGradeService;
	/**
	 * 
	 * <h2>跳转到会员列表页面</h2> 
	 * 
	 * @param request
	 * @return
	 * @return String 
	 * @throws
	 */
	@RequestMapping(value="/list")
	public String list(HttpServletRequest request,Model model){
		model.addAttribute("discount", memberGradeService.selectList());
		return "member/list";
	}
	@RequestMapping(value="/selectList")
	@ResponseBody
	public PageView selectList(HttpServletRequest request,String pageNow, String pageSize, String column, String sort){
		Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
		
		try{
			//查询用户列表
			String mPhone=request.getParameter("mPhone");
			String grade=request.getParameter("grade");
			ParamMap paramMap = getFormMap(ParamMap.class);
			paramMap.put("mPhone", mPhone);
			paramMap.put("grade", grade);			
			paramMap = toFormMap(paramMap, pageNow, pageSize,paramMap.getStr("orderby"));
			paramMap.put("column", column);
			paramMap.put("sort", sort);
//			List<Member> goodsList = this.memberService.selectList(paramMap);
			List<Member> goodsList = this.memberService.selectMemberList(paramMap);
			pageView.setRecords(goodsList);
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
			logger.info("查询商品数据成功!");
		}catch(Exception e){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.error("查询商品数据异常:"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
		return pageView;
	}
	@RequestMapping(value="/addOrEdit")
	public  String addaddOrEdit(String id,Model model){
		try {
			Member member=memberService.selectByPrimaryKey(id);
			model.addAttribute("discount", memberGradeService.selectList());
			model.addAttribute("member", member);
			return "/member/edit";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
		
	}
	@RequestMapping(value="/saveMember")
	@ResponseBody
	public String saveMember(HttpServletRequest request,Member member){
		try {
			String mId=member.getmId();
			String mPhone=member.getmPhone();
			Member memberSelect=null;
			if(mId==null||"".equals(mId)){
				memberSelect=memberService.selectByPhone(mPhone);
				if(memberSelect!=null){
					return "用户已存在！";
				}
				member.setmPwd(MD5Util.MD5(member.getmPwd()));
				member.setCreateTime(new Date());
				memberService.insertSelective(member);
				return "\"success\"";
			}else{
				memberService.updateByPrimaryKeySelective(member);
				return "\"success\"";
			}
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
	@ResponseBody
	@RequestMapping("deleteEntity")
	public String deleteEntity(){
		try {
			String ids = getPara("ids");
			String[] idArr=ids.split(",");
			for (String string : idArr) {
				Member member=new Member();
				member.setmId(string);
				member.setIsdelete(Constants.IS_DELETEB);
				member.setModifyTime(new Date());
				memberService.updateByPrimaryKeySelective(member);
			}
			return "\"success\"";
		} catch (Exception e) {
			logger.error("查询商品数据异常"+e);
			throw new ScBizException("查询商品数据异常！", e);
		}
	}
}